package com.example.dbdesign.service.impl;

import com.example.dbdesign.pojo.entity.User;
import com.example.dbdesign.pojo.vo.LoginUser;
import com.example.dbdesign.pojo.vo.voUser;
import com.example.dbdesign.service.UserService;
import com.example.dbdesign.mapper.UserMapper;
import com.example.dbdesign.utils.JwtUtil;
import com.example.dbdesign.utils.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService  {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private AuthenticationManager authenticationManager;

    @Autowired
    private RedisCache redisCache;


    @Override
    public User selectById(int id){
//        return userMapper.queryById(id);
        User user = userMapper.queryById(id);
        user.setPwd("MASK");
        return user;
    }

//    @Override
//    public voUser selectByUnoAndRole(String uno, int roleId){
//        User u = new User();
//        u.setUno(uno);
//        u.setRole(roleId);
//        User user = userMapper.queryByUnoAndRole(u);
//        voUser vouser = new voUser(user.getId(), user.getUno(), user.getCreateTime(), user.getInfo());
//        return vouser;
//    }

    @Override
    public Map<String, String> login(String uno, String password){
        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(uno, password);
//        System.out.println(authenticationToken + "666");
        Authentication authentication = authenticationManager.authenticate(authenticationToken);
//        System.out.println(authentication + "777");
        if(authentication == null) {
            throw new RuntimeException("用户名或密码错误");
        }
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        int id = loginUser.getUser().getId();
        String jwt = JwtUtil.createJWT(String.valueOf(id), 60 * 60 * 24 * 1000L);
        redisCache.setCacheObject(String.valueOf(id), loginUser);
        HashMap<String, String> map = new HashMap<>();
        map.put("token", jwt);
        map.put("role", String.valueOf(loginUser.getUser().getRole()));
        return map;
    }

    @Override
    public voUser register(User user){
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String pwd = encoder.encode(user.getPwd());
        user.setPwd(pwd);
        LoginUser lu = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        user.setId(lu.getUser().getId());
        System.out.println("开始插入");
        userMapper.insert(user);
        System.out.println(user);
        User result = userMapper.queryByUno(user.getUno());
        if(result != null){
            voUser vo = new voUser(result.getId(), result.getUno(), result.getCreateTime(), null);
            return vo;
        }else{
            return null;
        }
    }

    @Override
    public voUser update(User user){
        LoginUser u = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        User ud = u.getUser();
        ud.setInfo(user.getInfo());
        userMapper.updateById(ud);
        voUser vo = new voUser(ud.getId(), ud.getUno(), ud.getCreateTime(), ud.getInfo());
        return vo;
    }

    @Override
    public voUser updateById(User user){
        User ud = userMapper.queryById(user.getId());
        ud.setInfo(user.getInfo());
        userMapper.updateById(ud);
        return new voUser(ud.getId(), ud.getUno(), ud.getCreateTime(), ud.getInfo());
    }

    @Override
    public Boolean delete(int id){
        userMapper.deleteById(id);
        return true;
    }

    @Override
    public Boolean logout(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginuser = (LoginUser) authentication.getPrincipal();
        int id = loginuser.getUser().getId();
        redisCache.deleteObject(String.valueOf(id));
        return true;
    }

    @Override
    public List<User> selectByPage(Map<String, Object> map){
        return userMapper.queryByPage(map);
    }

    @Override
    public int calSum(){
        return userMapper.cal();
    }

    @Override
    public List<User> selectByTypeAndDate(int type, int date){
        Map<String, Object> map = new HashMap<>();
        map.put("type", type);
        map.put("date", date);
        if(type == 0) {
            return selectByDate(date);
        }
        if(date == 0) {
            return selectByType(type);
        }
        return userMapper.queryByTypeAndDate(map);
    }

    @Override
    public List<User> selectByType(int type){
        return userMapper.queryByType(type);
    }

    @Override
    public List<User> selectByDate(int date){
        return userMapper.queryByDate(date);
    }

    @Override
    public List<User> selectForTeacher(){
        LoginUser lu = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if(lu.getUser().getRole() == 2){
            return userMapper.selectStuByIdForTeacher(lu.getUser().getId());
        }else{
            return null;
        }
    }

}
